﻿namespace CachePage
{
    using System;
    using System.Runtime.CompilerServices;
    using System.Web;
    using System.Web.Caching;

    public class CacheUtility<T>
    {
        private Cache _cache;

        public CacheUtility(Cache cache)
        {
            this._cache = cache;
        }

        public T Get(string key, string dependencyPath, NullCase<T> isnull)
        {
            object obj2 = this._cache.Get(key);
            if ((obj2 == null) && (isnull != null))
            {
                obj2 = isnull(key);
                this.Insert(key, (T) obj2, dependencyPath);
            }
            return (T) obj2;
        }

        public T Get(string key, TimeSpan continueTime, NullCase<T> isnull)
        {
            object obj2 = this._cache.Get(key);
            if ((obj2 == null) && (isnull != null))
            {
                obj2 = isnull(key);
                this.Insert(key, (T) obj2, continueTime);
            }
            return (T) obj2;
        }

        public static T Get(Cache cache, string key, string dependencyPath, NullCase<T> isnull)
        {
            CacheUtility<T> utility = new CacheUtility<T>(cache);
            return utility.Get(key, dependencyPath, isnull);
        }

        public static T Get(Cache cache, string key, TimeSpan continueTime, NullCase<T> isnull)
        {
            CacheUtility<T> utility = new CacheUtility<T>(cache);
            return utility.Get(key, continueTime, isnull);
        }

        public static T GetByContext(string key, string dependencyPath, NullCase<T> isnull)
        {
            CacheUtility<T> utility = new CacheUtility<T>(HttpContext.Current.Cache);
            return utility.Get(key, dependencyPath, isnull);
        }

        public static T GetByContext(string key, TimeSpan continueTime, NullCase<T> isnull)
        {
            CacheUtility<T> utility = new CacheUtility<T>(HttpContext.Current.Cache);
            return utility.Get(key, continueTime, isnull);
        }

        public void Insert(string key, T value, string dependencyPath)
        {
            if (value != null)
            {
                this._cache.Insert(key, value, new CacheDependency(dependencyPath));
            }
            else
            {
                this._cache.Remove(key);
            }
        }

        public void Insert(string key, T value, TimeSpan continueTime)
        {
            if (value != null)
            {
                this._cache.Insert(key, value, null, Cache.NoAbsoluteExpiration, continueTime);
            }
            else
            {
                this._cache.Remove(key);
            }
        }

        public static void Insert(Cache cache, string key, T value, string dependencyPath)
        {
            new CacheUtility<T>(cache).Insert(key, value, dependencyPath);
        }

        public static void Insert(Cache cache, string key, T value, TimeSpan continueTime)
        {
            new CacheUtility<T>(cache).Insert(key, value, continueTime);
        }

        public static void InsertByContext(string key, T value, string dependencyPath)
        {
            new CacheUtility<T>(HttpContext.Current.Cache).Insert(key, value, dependencyPath);
        }

        public static void InsertByContext(string key, T value, TimeSpan continueTime)
        {
            new CacheUtility<T>(HttpContext.Current.Cache).Insert(key, value, continueTime);
        }

        public delegate T NullCase(string key);
    }
}

